package com.loveyukari.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.loveyukari.dao.AuthInvitationCodeDao;
import com.loveyukari.model.AuthInvitationCode;

/**
*
*yangshaohua
*2017年2月14日下午4:21:57
**/
@Repository
public class JdbcAuthInvitationCodeDao extends BaseDao implements AuthInvitationCodeDao{

	
	private class AuthInvitationCodeRowMapper implements RowMapper<AuthInvitationCode>{

		public AuthInvitationCode mapRow(ResultSet rs, int rowNum) throws SQLException {
			AuthInvitationCode code = new AuthInvitationCode();
			code.setCode(rs.getString("code"));
			code.setStatus(rs.getInt("status"));
			code.setAuthId(rs.getInt("auth_id"));
			return code;
		}
		
	}
	
	public AuthInvitationCode selectByCode(String code) {
		String sql = "select * from auth_invitation_code where code = ?";
		List<AuthInvitationCode> list = this.getJdbcTemplate().query(sql, new AuthInvitationCodeRowMapper(),code);
		if (list.isEmpty()){
			return null;
		} else {
			return list.get(0);
		}
	}

	public void updateCode(AuthInvitationCode code) {
		String sql = "update auth_invitation_code set status = ?,auth_id = ? where code = ?";
		this.getJdbcTemplate().update(sql,code.getStatus(),code.getAuthId(),code.getCode());
	}
	

}
